/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.adou.common.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author crg
 */
public class CompressPic {

    /**
     * 
     * 缩略图类（通用） 本java类能将jpg、bmp、png、gif图片文件，进行等比或非等比的大小转换。 具体使用方法
     * compressPic(大图片路径,生成小图片路径,大图片文件名,生成小图片文名,生成小图片宽度,生成小图片高度,是否等比缩放(默认为true))
     */
    private int outputWidth; // 默认输出图片宽
    private int outputHeight; // 默认输出图片高
    private boolean proportion; // 是否等比缩放标记(默认为等比缩放)

    /**
     *
     */
    public CompressPic() { // 初始化变量
        outputWidth = 120;
        outputHeight = 120;
        proportion = true;
    }

    // 图片处理

    /**
     *
     * @param inputDir
     * @param outputDir
     * @param inputFileName
     * @param outputFileName
     * @return
     */
    public int compress(String inputDir, String outputDir, String inputFileName, String outputFileName) {
        try {
            // 获得源文件
            File file = new File(inputDir + inputFileName);
            //System.out.println(inputDir + inputFileName);
            if (!file.exists()) {
                System.out.println(" 文件不存在!" + "<BR>");
                return -1;
            } else {
                Image img = ImageIO.read(file);
                return start(img, outputDir, outputFileName);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            return -3;
        }
    }

    /**
     *
     * @param in
     * @param outputDir
     * @param outputFileName
     * @return
     */
    public int compress(InputStream in, String outputDir, String outputFileName) {
        try {
            Image img = ImageIO.read(in);
            return start(img, outputDir, outputFileName);
        } catch (IOException ex) {
            ex.printStackTrace();
            return -3;
        }
    }

    /**
     *
     * @param inputDir
     * @param outputDir
     * @param inputFileName
     * @param outputFileName
     * @param width
     * @param height
     * @param gp
     * @return
     */
    public int compress(String inputDir, String outputDir, String inputFileName, String outputFileName, int width, int height, boolean gp) {
        outputWidth = width;
        outputHeight = height;
        proportion = gp;
        try {
            // 获得源文件
            File file = new File(inputDir + inputFileName);
            //System.out.println(inputDir + inputFileName);
            if (!file.exists()) {
                System.out.println(" 文件不存在!" + "<BR>");
                return -1;
            } else {
                Image img = ImageIO.read(file);
                return start(img, outputDir, outputFileName);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            return -3;
        }
    }

    /**
     *
     * @param in
     * @param outputDir
     * @param outputFileName
     * @param width
     * @param height
     * @param gp
     * @return
     */
    public int compress(InputStream in, String outputDir, String outputFileName, int width, int height, boolean gp) {
        outputWidth = width;
        outputHeight = height;
        proportion = gp;
        try {
            Image img = ImageIO.read(in);
            return start(img, outputDir, outputFileName);
        } catch (IOException ex) {
            ex.printStackTrace();
            return -3;
        }
    }

    private int start(Image img, String outputDir, String outputFileName) {
        // 判断图片格式是否正确
        try {
            int newWidth = 0;
            int newHeight = 0;
            if (img.getWidth(null) == -1) {
                System.out.println(" 不能读取图像文件大小!" + "<BR>");
                return -2;
            } else if (img.getWidth(null) <= outputWidth && img.getHeight(null) <= outputHeight) {
                newWidth = img.getWidth(null); // 输出的图片宽度
                newHeight = img.getHeight(null); // 输出的图片高度
            } else // 判断是否是等比缩放
            {
                if (proportion == true) {
                    // 为等比缩放计算输出的图片宽度及高度
                    double rate1 = ((double) img.getWidth(null))
                            / (double) outputWidth;
                    double rate2 = ((double) img.getHeight(null))
                            / (double) outputHeight;
                    // 根据缩放比率大的进行缩放控制
                    double rate = rate1 > rate2 ? rate1 : rate2;
                    newWidth = (int) (((double) img.getWidth(null)) / rate);
                    newHeight = (int) (((double) img.getHeight(null)) / rate);
                } else {
                    newWidth = outputWidth; // 输出的图片宽度
                    newHeight = outputHeight; // 输出的图片高度
                }
            }
            BufferedImage tag = new BufferedImage((int) newWidth,
                    (int) newHeight, BufferedImage.TYPE_INT_RGB);
            
            /**
             * Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
             */
            tag.getGraphics().drawImage(
                    img.getScaledInstance(newWidth, newHeight,
                            Image.SCALE_SMOOTH), 0, 0, null);
            File f = new File(outputDir);
            if (!f.exists()) {
                f.mkdirs();
            }
            String formatName = outputFileName.substring(outputFileName.lastIndexOf(".") + 1);
            ImageIO.write(tag, formatName, new File(outputDir + outputFileName));
        } catch (IOException ex) {
            ex.printStackTrace();
            return -4;
        }
        return 2;
    }
}
